package com.example.leero.graduation.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.leero.graduation.API;
import com.example.leero.graduation.App;
import com.example.leero.graduation.R;
import com.example.leero.graduation.Util.GsonUtil;
import com.example.leero.graduation.Util.LogUtil;
import com.example.leero.graduation.adapter.ActUserJoinAdapter;
import com.example.leero.graduation.model.MineAct;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class UserPublishActivity extends AppCompatActivity {

    private RecyclerView rvActList;
    private ActUserJoinAdapter adapter;
    private List<MineAct> actList = new ArrayList();
    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_publish);

        Intent intent = getIntent();
        userId = intent.getIntExtra("user_id", 1);

        setupRv();
        getData();

        TextView title = (TextView) findViewById(R.id.tv_title);
        title.setText("发布的活动");
        findViewById(R.id.iv_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void setupRv() {
        rvActList = (RecyclerView) findViewById(R.id.rv_join_list);
        rvActList.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ActUserJoinAdapter(this, R.layout.item_act_list, actList);
        rvActList.setAdapter(adapter);
    }

    private void getData() {
        OkHttpUtils.post().url(API.USER_PUBLISH).addParams("user_id", userId + "")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }
            @Override
            public void onResponse(String response, int id) {
                actList.clear();
                actList.addAll(GsonUtil.jsonToArrayList(GsonUtil.getData(response), MineAct.class));
                adapter.notifyDataSetChanged();
            }
        });
    }
}
